package com.tbit.uqbike.webmanager.controller;

import com.github.pagehelper.PageInfo;
import com.tbit.uqbike.object.pojo.BikeModel;
import com.tbit.uqbike.object.value.Page;
import com.tbit.uqbike.object.view.JsonResult;
import com.tbit.uqbike.service.business.BikeModelService;
import com.tbit.uqbike.webmanager.util.PageHelperUtil;
import com.tbit.utils.Assert;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;

/**
 * 车辆型号表(BikeModel)表控制层
 *
 * @author jing
 * @since 2020-12-04 10:14:30
 */
@RestController
@RequestMapping("/bikemodel")
public class BikeModelController {
    /**
     * 服务对象
     */
    @Resource
    private BikeModelService bikeModelService;

    /**
     * 通过主键查询单条数据
     *
     * @param bikeModelId 主键
     * @return 单条数据
     */
    @PostMapping("/selectList")
    public JsonResult selectList(Integer bikeModelId) {
        return JsonResult.ok(this.bikeModelService.queryById(bikeModelId));
    }

    @PostMapping("/add")
    public JsonResult add(BikeModel bikeModel) {
        /* 校验 */
        Assert.notNull(bikeModel.getModel(), "ver.model.notNull");
        Assert.notNull(bikeModel.getBikeModelNO(), "ver.bikeModelNO.notNull");
        Assert.hasLength(bikeModel.getFactory(), "ver.factory.notNull");
        bikeModel.setAddTime(new Date());
        bikeModelService.insert(bikeModel);
        return JsonResult.ok();
    }

    @PostMapping("/edit")
    public JsonResult edit(BikeModel bikeModel) {
        Assert.notNull(bikeModel.getBikeModelId(), "ver.bikeModelId.notNull");
        Assert.notNull(bikeModel.getModel(), "ver.model.notNull");
        Assert.notNull(bikeModel.getBikeModelNO(), "ver.bikeModelNO.notNull");
        Assert.hasLength(bikeModel.getFactory(), "ver.factory.notNull");
        bikeModelService.update(bikeModel);
        return JsonResult.ok();
    }

    @PostMapping("/delete")
    public JsonResult del(BikeModel bikeModel) {
        Assert.notNull(bikeModel.getBikeModelId(), "ver.bikeModelId.notNull");
        bikeModelService.deleteById(bikeModel.getBikeModelId());
        return JsonResult.ok();
    }

    @PostMapping("/queryPage")
    public JsonResult queryPage(BikeModel bikeModel) {
        bikeModel.setPage(new Page(bikeModel.getPageNO(), bikeModel.getRowCount()));
        PageHelperUtil.initPage(bikeModel.getPage());
        PageInfo pageInfo = new PageInfo(bikeModelService.queryPage(bikeModel));
        return JsonResult.ok(pageInfo.getList(), (int) pageInfo.getTotal());
    }

}